package com.ruoyi.coupon.job;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.flashpay.constant.CacheKey;
import com.ruoyi.coupon.domain.TbCoupon;
import com.ruoyi.coupon.service.ITbCouponService;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;


@Component
public class CacheCouponJob {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private ITbCouponService tbCouponService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    //0 0 0 * * ?   每天凌晨0点触发定时器
    @Scheduled(cron = "0 0 0 * * ?")
    public void syncCache() {
        //获取第二天的时间
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, 1);
        //获取第二天生效的优惠券集合
        List<TbCoupon> tbCouponList = tbCouponService.selectCurrentDayTbCouponList(calendar.getTime());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (TbCoupon tbCoupon : tbCouponList) {
            Map<String, Object> tbCouponMap = BeanUtil.beanToMap(tbCoupon);
            tbCouponMap.entrySet().removeIf(coupon -> coupon.getValue() == null);
            tbCouponMap.put("num", "0");
            tbCouponMap.put("couponId", String.valueOf(tbCoupon.getCouponId()));
            tbCouponMap.put("couponTypeId", String.valueOf(tbCoupon.getCouponTypeId()));
            tbCouponMap.put("couponTypeId", String.valueOf(tbCoupon.getCouponTypeId()));
            tbCouponMap.put("statTime", sdf.format(tbCoupon.getStatTime()));
            tbCouponMap.put("endTime", sdf.format(tbCoupon.getEndTime()));
            tbCouponMap.put("faceValue", tbCoupon.getFaceValue().toString());
            tbCouponMap.put("minPoint", tbCoupon.getMinPoint().toString());
            tbCouponMap.remove("receivedNum");
            tbCouponMap.remove("usedNum");
            tbCouponMap.remove("createTime");
            tbCouponMap.remove("params");
            stringRedisTemplate.opsForHash().putAll(CacheKey.getCouponKey(tbCoupon.getCouponId()), tbCouponMap);
            //获取当前时间毫秒值
            Long timeMillis = tbCoupon.getStatTime().getTime() - System.currentTimeMillis();
            //发送延时队列
            rabbitTemplate.convertAndSend("delay-exchange", null, tbCoupon.getCouponId(), new MessagePostProcessor() {
                @Override
                public Message postProcessMessage(Message message) throws AmqpException {
                    //timeMillis.intValue()   原本时间
                    message.getMessageProperties().setDelay(timeMillis.intValue());
                    return message;
                }
            });

        }


    }

}
